<?php
    include(dirname(__FILE__)."/../../../Dao/db.php");
    function campaignLoadData()
    {
        //キャンペーン情報を取得
        $query = "SELECT * FROM campaign WHERE ";
        $query .= "camp_Id=".$_GET['campaignId'];

        $result = mysql_query($query);
        while($row=mysql_fetch_array($result))
        {
            $campaignName = $row['name'];
            $campaignSummarize = $row['summarize'];
            $start = $row['startDay'];
            if(isset($row['endDay']))
            {
                $endDate = substr($row['endDay'],0,10);
                $endHour = substr($row['endDay'],11,2);
                $endMin = substr($row['endDay'],14,2);
                $endSec = substr($row['endDay'],17,2);
            }
            else
            {
                $endDate = "";
                $endHour = "00";
                $endMin = "00";
                $endSec = "00";
            }
            $networkTargeting = $row['networkTargeting'];
            $isEnableSeperateContentBids = $row['enableSeparateContentBids'];
            $isEnableAutoOptimization = $row['enableBudgetOptimizer'];
            $status = $row['status'];
            $dailyBudget = $row['dailyBudget'];
        }

        //アラーと情報を取得
        $query = "SELECT * FROM alertschedule WHERE campaignId={$_GET['campaignId']}";
        $result = mysql_query($query);
        while($row=mysql_fetch_array($result))
        {
            $budgetSpan = $row['period'];
            $budget = $row['alertBudget'];
            $budgetLimit = $row['conversionPremium'];
        }

        $script ="<script language='javascript'>";
        $script .= "document.inputform.campaign_name.value=\"".$campaignName."\";";
        $script .= "document.inputform.campaign_summarize.value=\"".$campaignSummarize."\";";
        $script .= "document.getElementById('start_date_display').innerHTML=\"".$start."\";";
        $script .= "document.inputform.start_date.value=\"".$start."\";";
        $script .= "document.inputform.end_date.value=\"".$endDate."\";";
        $script .= "document.inputform.end_hour.value=\"".$endHour."\";";
        $script .= "document.inputform.end_min.value=\"".$endMin."\";";
        $script .= "document.inputform.end_sec.value=\"".$endSec."\";";

        $networkTargeting = split(",",$networkTargeting);
        foreach($networkTargeting as $nt)
        {
            if($nt == "GoogleSearch")
            {
                $script .= "document.inputform.googleSearch.checked=true;";
                $script .= "document.inputform.partnerSearch.disabled=false;";
            }
            else if($nt == "SearchNetwork")
            {
                $script .= "document.inputform.googleSearch.checked=true;";
                $script .= "document.inputform.partnerSearch.disabled=false;";
                $script .= "document.inputform.partnerSearch.checked=true;";
            }
            else if($nt == "ContentNetwork")
            {
                $script .= "document.inputform.contentSearch.checked=true;";
            }
        }

        if($isEnableSeperateContentBids == 1)
        {
            $script .= "document.inputform.enableContentBids.checked=true;";
        }

        if($isEnableAutoOptimization == 1)
        {
            $script .= "document.inputform.auto_optimization.checked=true;";
        }

        if($status == 1)
        {
            $script .= "document.inputform.status[0].checked=true;";
        }
        else
        {
            $script .= "document.inputform.status[1].checked=true;";
        }

        $script .= "document.inputform.budget_daily.value=\"".$dailyBudget."\";";
        if($budgetSpan == 3)
        {
            $script .= "document.inputform.budget_span[0].checked=true;";
        }
        else if($budgetSpan == 9)
        {
            $script .= "document.inputform.budget_span[1].checked=true;";
        }
        else
        {
            $script .= "document.inputform.budget_span[2].checked=true;";
        }
        
        $script .= "document.inputform.budget.value=\"".$budget."\";";
        $script .= "document.inputform.budget_limit.value=\"".$budgetLimit."\";";
        $script .= "</script>";
        echo $script;
    }

    function adGroupLoadData()
    {
        //メディア種類を取得
        $query = "SELECT name FROM listingmst WHERE ";
        $query .= "id={$_GET['listingType']}";
        $result = mysql_query($query);
        $row=mysql_fetch_array($result);
        $listingName=$row['name'];

        //該当広告グループの情報を取得
        $query = "SELECT * FROM adgroup WHERE ";
        $query .= "adGr_Id={$_GET['adGroupId']}";
        $result = mysql_query($query);
        $row=mysql_fetch_array($result);
        $adGroupName = $row['name'];
        $landingUrl = $row['landingURL'];
        $countType = $row['countType'];
        $maxCpc = $row['maxCpc'];
        if(isset($row['maxContentCpc']))
        {
            $maxContentCpc = $row['maxContentCpc'];
        }
        else
        {
            $maxContentCpc = 0;
        }
        $status = $row['status'];

        $query = "SELECT text FROM keyword WHERE ";
        $query .= "adGr_Id={$_GET['adGroupId']} AND ";
        $query .= "negative=1 AND ";
        $query .= "status!=9";

        $result = mysql_query($query);
        $keywords = array();
        while($row=mysql_fetch_array($result))
        {
            array_push($keywords,$row['text']);
        }

        $Sexcluded_word_list = "";
        for($i=0;$i<count($keywords)-1;$i++)
        {
            $Sexcluded_word_list .=$keywords[$i].",";
        }
        $Sexcluded_word_list .= $keywords[count($keywords)-1];

        $script ="<script language='javascript'>";
        $script .= "document.getElementById('listing_name_display').innerHTML=\"".$listingName."\";";
        $script .= "document.inputform.listing_name.value=\"".$listingName."\";";
        $script .= "document.inputform.adgroup_name.value=\"".$adGroupName."\";";
        $script .= "document.inputform.landing_url.value=\"".$landingUrl."\";";

        if($countType == 0)
        {
            $script .= "document.inputform.count_by_creative[0].checked=true;";
        }
        else
        {
            $script .= "document.inputform.count_by_creative[1].checked=true;";
        }

        $script .= "document.inputform.sponsored_maxbid.value=$maxCpc;";
        $script .= "document.inputform.content_maxbid.value=$maxContentCpc;";

        if($Sexcluded_word_list != "")
        {
        $script .= "document.getElementById('excluded_word_list').innerHTML=\"".$Sexcluded_word_list."\";";
        }
        $script .= "document.inputform.excluded_word_list.value=\"".$Sexcluded_word_list."\";";

        if($status == "1")
        {
            $script .= "document.inputform.status[0].checked=true;";
        }
        else
        {
            $script .= "document.inputform.status[1].checked=true;";
        }

        $script .= "</script>";
        echo $script;
    }

    function creativeLoadData()
    {
        //メディア名を取得
        $query = "SELECT b.name FROM ";
        $query .= "campaign a JOIN listingmst b ON a.listingType=b.id WHERE ";
        $query .= "a.camp_Id={$_GET['campaignId']}";

        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $listingName = $row['name'];

        $query = "SELECT * FROM creative WHERE ";
        $query .= "Crt_Id=".$_GET['creativeId'];

        $result = mysql_query($query);
        while($row=mysql_fetch_array($result))
        {
            $creativeName = $row['name'];
            $title = $row['headline'];
            $descriptions = $row['description1']."\\r\\n".$row['description2'];

            $displayUrl = $row['displayUrl'];
            $destinationUrl = $row['destinationUrl'];
            $status = $row['status'];
        }

        $script ="<script language='javascript'>";
        $script .= "document.getElementById('listing_name_display').innerHTML=\"".$listingName."\";";
        $script .= "document.inputform.listing_name.value=\"".$listingName."\";";

        $script .= "document.inputform.creative_name.value=\"".$creativeName."\";";

        $script .= "document.inputform.title.value=\"".$title."\";";

        $script .= "document.inputform.short_description.value=\"".$descriptions."\";";

        $script .= "document.inputform.display_url.value=\"".$displayUrl."\";";

        $script .= "document.inputform.landing_url_creative.value=\"".$destinationUrl."\";";

        if($status == "1")
        {
            $script .= "document.inputform.status[0].checked=true;";
        }
        else
        {
            $script .= "document.inputform.status[1].checked=true;";
        }

        $script .= "</script>";
        echo $script;
    }

    function keywordLoadData()
    {
         //メディア名を取得
        $query = "SELECT name FROM listingmst WHERE ";
        $query .= "id={$_GET['listingType']}";

        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $listingName = $row['name'];


        $query = "SELECT * FROM keyword WHERE ";
        $query .= "key_Id={$_GET['keywordId']}";

        $result = mysql_query($query);
        while($row=mysql_fetch_array($result))
        {
            $keywordName = $row['text'];
            $replaceText = $row['replaceText'];
            $duplicate = $row['duplicate'];
            $matchType = $row['type'];
            $maxCpc = $row['maxCpc'];
            $isPaused = $row['paused'];
            $destinationUrl = $row['destinationUrl'];
        }

        $script ="<script language='javascript'>";
        $script .= "document.getElementById('listing_name_display').innerHTML=\"".$listingName."\";";
        $script .= "document.inputform.listing_name.value=\"".$listingName."\";";

        $script .= "document.getElementById('keyword_name_display').innerHTML=\"".$keywordName."\";";
        $script .= "document.inputform.keyword_name.value=\"".$keywordName."\";";

        $script .= "document.inputform.alternate_text.value=\"".$replaceText."\";";

        if($matchType == 2)
        {
            $script .= "document.inputform.sponsored_search[0].checked=true;";
        }
        else
        {
            $script .= "document.inputform.sponsored_search[1].checked=true;";
        }

        $script .= "document.inputform.sponsored_maxbid.value=\"".$maxCpc."\";";

        $script .= "document.inputform.landing_url_keyword.value=\"".$destinationUrl."\";";

        if($isPaused == 0)
        {
            $script .= "document.inputform.status[0].checked=true;";
        }
        else
        {
            $script .= "document.inputform.status[1].checked=true;";
        }

        $script .= "</script>";
        echo $script;
    }
?>
